package com.iwdnb.gkgz.ui.controller;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.gkgz.application.service.HistoryDataService;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.application.vo.StockType;
import com.iwdnb.gkgz.common.utils.StockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 股票历史数据处理
 *
 * @author pengl
 * @date 2020/05/03
 **/
@RestController
@RequestMapping("/api/stock/history")
public class StockHistoryDataController {

    @Autowired
    HistoryDataService historyDataService;

    @RequestMapping("/hs/loadStockList")
    public Result loadStockList(){
        historyDataService.loadStockDataList(StockType.SZ);
        historyDataService.loadStockDataList(StockType.SH);

        return Result.getSuccessResult(null);
    }


    @RequestMapping("/loadStockDayData")
    public Result<String> loadStockDayData(@RequestParam("year") String year,
        @RequestParam("code") String code){
        historyDataService.loadStockDayData(code,year);
        return Result.getSuccessResult(null);
    }

    @RequestMapping("/year/list")
    public Result<List<StockDayData>> listYearData(@RequestParam("year") String year,
                                                  @RequestParam("code") String code){
        int yearVal = Integer.valueOf(year);
//        Calendar instance = Calendar.getInstance();
//        int curYear = instance.get(Calendar.YEAR);
//        if(curYear - yearVal > 5){
//            return ResultUtil.error("year must less than 5 year.");
//        }

        List<StockDayData> dayData = historyDataService.getDayData(code, yearVal);
        return Result.getSuccessResult(dayData);
    }

    @RequestMapping("/day/query")
    public Result<List<StockDayData>> queryData(@RequestParam("startDate") String startDateStr,
                                                @RequestParam(value = "endDate", required = false) String endDateStr,
                                                @RequestParam("code") String code){
        Date startDate = DateUtil.parseDate(startDateStr);

        Date endDate = new Date();
        if(!StringUtils.isEmpty(endDateStr)){
            endDate = DateUtil.parseDate(endDateStr);
        }

        List<StockDayData> dayData = historyDataService.getDataByRange(code, startDate, endDate);
        return Result.getSuccessResult(dayData);
    }


//    @RequestMapping("/day/calc")
//    public Result<List<StockDayDataResultDO>> calcData(@RequestParam("startDate") String startDateStr,
//                                                @RequestParam(value = "endDate", required = false) String endDateStr,
//                                                @RequestParam("code") String code){
//        Date startDate = DateUtil.string2Date(startDateStr,"yyyy-MM-dd");
//
//        Date endDate = new Date();
//        if(!StringUtils.isEmpty(endDateStr)){
//            endDate = DateUtil.string2Date(endDateStr,"yyyy-MM-dd");
//        }
//
//        List<StockDayDataResultDO> dayData = historyDataService.calcDataByRange(code, startDate, endDate);
//        return Result.getSuccessResult(dayData);
//    }



}
